package org.example.config;

import org.example.vo.DeptVo;
import org.example.vo.MenuVo;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class DeptTree {
    /**
     * 获取父节点
     */
    public static List<DeptVo> toTree(List<DeptVo> list) {
        return list.stream().filter(
                m -> Objects.equals(m.getParentId(), 0)
        ).peek(
                (m) -> m.setDeptVos(getChildren(m, list))
        ).collect(Collectors.toList());
    }

    /**
     * 递归查询子节点
     *
     * @param linkAge
     * @param list
     * @return
     */
    public static List<DeptVo> getChildren(DeptVo linkAge, List<DeptVo> list) {

        return list.stream().filter(m -> Objects.equals(m.getParentId(), linkAge.getId()))
                .peek(
                        (m) -> {
                            if (!getChildren(m, list).isEmpty()) {
                                m.setDeptVos(getChildren(m, list));
                            }
                        }).collect(Collectors.toList());
    }

}
